package com.yanqu.road.server.manger.activity.snowgarden;

import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.game.service.GameService;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.apprentice.ApprenticeMgr;
import com.yanqu.road.server.manger.activity.snowgarden.service.ActivityMallDayBuyAutoRewardServiceCmd;
import com.yanqu.road.server.manger.activity.snowgarden.service.ActivityMallDayBuyRefreshServiceCmd;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全服团购条件
 */
public class ActivityMallDayBuyMgr extends TempMgr {
    private static Map<Integer, MallDayBuyActivity> activityMap = new ConcurrentHashMap<>();


    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadActivityData();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload ActivityMallDayBuyMgr start");

        for (ActivityInfo activityInfo : getAllActivityInfoList()) {
            getActivity(activityInfo.getActivityId());
        }

        getLogger().info("reload ActivityMallDayBuyMgr end");
    }

    @Override
    public boolean save() {

        //移除数据
        List<Integer> toRemoveList = new ArrayList<>();

        for (MallDayBuyActivity activity : activityMap.values()) {
            activity.save();

            ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activity.getActivityId());
            if(activityInfo == null){
                toRemoveList.add(activity.getActivityId());
            }
        }

        for (Integer activityId : toRemoveList) {
            //移除会影响补发逻辑吗
        }

        return true;
    }

    public static List<ActivityInfo> getAllActivityInfoList(){
        List<ActivityInfo> list = new ArrayList<>();
        ActivityInfo activityInfo = SnowGardenMgr.getActivityInfo();
        if(activityInfo != null){
            list.add(activityInfo);
        }
        activityInfo = SummerGamesActivityMgr.getActivityInfo();
        if(activityInfo != null){
            list.add(activityInfo);
        }
        //爱徒养成
        Map<Integer, ApprenticeConfig> apprenticeConfigMap = ApprenticeMgr.getConfigMap();
        for (ApprenticeConfig apprenticeConfig : apprenticeConfigMap.values()) {
            list.add(apprenticeConfig.getActivityInfo());
        }
        //其他活动继续加

        return list;
    }

    public static void initService(GameService syncMessageService) {
        syncMessageService.addGameServiceCommand(GameProtocol.S_SERVER_BUY_COUNT_REFRESH, new ActivityMallDayBuyRefreshServiceCmd());
        syncMessageService.addGameServiceCommand(GameProtocol.S_SERVER_BUY_DAY_AUTO_REWARD_NOTIFY, new ActivityMallDayBuyAutoRewardServiceCmd());
    }

    public static MallDayBuyActivity getActivity(int activityId) {
        boolean exist = false;
        for (ActivityInfo activityInfo : getAllActivityInfoList()) {
            if(activityId == activityInfo.getActivityId()){
                exist = true;
                break;
            }
        }
        if(!exist){
            return null;
        }
        MallDayBuyActivity activity = activityMap.get(activityId);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(activityId);
                if (activity == null) {
                    activity = new MallDayBuyActivity(activityId);
                    activityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    public static void addMallBuy(int activityId, int mallId, long userId) {
        MallDayBuyActivity activity = getActivity(activityId);
        if (activity == null) {
            return;
        }
        activity.addMallBuy(userId, mallId);
    }

    public static void syncDataTask() {
        List<ActivityInfo> allActivityInfoList = getAllActivityInfoList();
        for (ActivityInfo activityInfo : allActivityInfoList) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                MallDayBuyActivity activity = getActivity(activityInfo.getActivityId());
                if (activity != null) {
                    activity.syncServerBuyCount(); //同步数据上去
                    activity.queryActivityTotalCountTask(); //请求数据回来
                }
            }
        }
    }

    public static boolean isUserTodayBuyMall(int activityId, long userId, int mallId) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            return false;
        }
        int whichDay = ActivityHelper.getWhichDayAfterBeginTime(openActivityInfo);
        MallDayBuyActivity activity = getActivity(openActivityInfo.getActivityId());
        if (activity == null) {
            return false;
        }
        return activity.isUserTodayBuyMall(whichDay, userId, mallId);
    }

    public static long getActivityMallDayBuyTotalCount(int activityId, int mallId) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            return 0;
        }
        int whichDay = ActivityHelper.getWhichDayAfterBeginTime(openActivityInfo);
        return getActivityMallDayBuyTotalCount(activityId, whichDay, mallId);
    }

    public static long getActivityMallDayBuyTotalCount(int activityId, int day, int mallId) {
        MallDayBuyActivity activity = getActivity(activityId);
        if (activity == null) {
            return 0;
        }
        return activity.getTotalDayBuyCount(day, mallId);
    }

    public static void updateGetConditionReward(int activityId, long userId, int mallId, int conditionId) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            return;
        }
        int whichDay = ActivityHelper.getWhichDayAfterBeginTime(openActivityInfo);
        MallDayBuyActivity activity = getActivity(openActivityInfo.getActivityId());
        if (activity == null) {
            return;
        }
        activity.updateGetConditionReward(whichDay, userId, mallId, conditionId);
    }
}
